package com.astersoft.lib.db;

/*
 * Copyright 2012 AsterSoft personal developer - Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 * Class that manage connection acquired
 * from generic AS application context
 * 
 * Version History:
 * 
 * @author dario durzo
 * @version 0.1
 * @date 28-05-2012
 * 
 */
public class ASConnectionManager {
	private Context ctx;
	private DataSource source;

	/**
	 * Construct connection manager
	 */
	public ASConnectionManager(){
		// do nothing
	}

	/**
	 * Get generic connection
	 * 
	 * @param schema 
	 * 			The schema name
	 * @return 
	 * 			The generic connection
	 */
	public Connection getConnection(String schema) {
		return getConnection(schema, false, -1);
	}
	public Connection getConnection(String schema, boolean autocommit, int transactionIsolation) {
		Connection conn = null;
		try{
			this.ctx = new InitialContext();
			if(ctx == null ) 
				throw new Exception("Boom - No Context");

			source = (DataSource)(this.ctx).lookup("java:comp/env/" + schema);
			
			if (source != null) {
				conn = source.getConnection();
				conn.setAutoCommit(autocommit);
				
				if(transactionIsolation > -1) {
					conn.setTransactionIsolation(transactionIsolation);
				}
			}
			return conn;
		}catch (Exception e) {
			e.printStackTrace();
			cleanUp(conn);
		}	
		return null;
	}

	/**
	 * Close and clean connection elements
	 * 
	 * @param conn
	 * 		The Connection to close
	 * @param ps
	 * 		The PreparedStatemente to close
	 * @param rs
	 * 		The ResultSet to close
	 */
	public void cleanUp(Connection conn, Statement ps, ResultSet rs){
		try{
			if(conn != null)
				conn.close();
		}catch(SQLException e){
			// do nothing
		}

		try{
			if(ps != null)
				ps.close();
		}catch(SQLException e){
			// do nothing
		}

		try{
			if(rs != null)
				rs.close();
		}catch(SQLException e){
			// do nothing
		}
	}

	/** OVERLOADE METHODS **/
	public void cleanUp(Connection conn) {
		cleanUp(conn, null, null);
	}

	public void cleanUp(Statement st) {
		cleanUp(null, st, null);
	}

	public void cleanUp(ResultSet rs) {
		cleanUp(null, null, rs);
	}

	public void cleanUp(Connection conn, Statement st) {
		cleanUp(conn, st, null);
	}

	public void cleanUp(Connection conn, ResultSet rs) {
		cleanUp(conn, null, rs);
	}

	public void cleanUp(Statement st, ResultSet rs) {
		cleanUp(null, st, rs);
	}
}
